import { USER_DICT_DATA } from '@/constants'
import { defineStore } from 'pinia'

export const useDictStore = defineStore({
  id: 'dictStore',
  state: () => ({
    dict: [],
  }),
  actions: {
    // 根据key获取字典值
    getDict(key) {
      if (key == null && key === '') {
        return null
      }
      try {
        for (let i = 0; i < this.dict.length; i++) {
          if (this.dict[i].key === key) {
            return this.dict[i].value
          }
        }
      }
      catch (e) {
        return null
      }
    },
    // 设置字典值
    setDict(key, value) {
      if (key !== null && key !== '') {
        this.dict.push({
          key,
          value,
        })
      }
    },
  },
  persist: {
    key: USER_DICT_DATA,
  },
})
